package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.bean.ShoppingCart;
import com.itheima.bean.User;
import com.itheima.exception.MyException;
import com.itheima.mapper.ShoppingCartMapper;
import com.itheima.service.ShoppingCartService;
import com.itheima.utils.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author hebing
 * @version 1.0
 */
@Service
@Transactional
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    /**
     * 添加
     *
     * @param shoppingCart
     * @return
     */
    @Override
    public int add(ShoppingCart shoppingCart, User user) {
        //获取当前登录用户 给购物车对象赋值
        shoppingCart.setUserId(user.getId());
        //获取dishId 判断是否存在
        Long dishId = shoppingCart.getDishId();
        //构建查询条件
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //设置当前登录用户id
        lqw.eq(ShoppingCart::getUserId, user.getId());
        //判断
        if (dishId != null) {
            //说明是添加普通餐 添加上dishId条件
            lqw.eq(ShoppingCart::getDishId, dishId);
        } else {
            //说明是套餐 添加上套餐条件
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //查询当前菜品是否在购物车中
        ShoppingCart cart = shoppingCartMapper.selectOne(lqw);
        //判断
        if (cart != null) {
            //有值 数量加一
            Integer number = cart.getNumber();
            //赋值
            cart.setNumber(number + 1);
            //修改数据库
            return shoppingCartMapper.updateById(cart);
        } else {
            //没值 直接插入一条记录
            //设置数量
            shoppingCart.setNumber(1);
            //设置创建时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            //插入一条记录
            return shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> findCart() {
        //设置查询条件
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //设置用户为当前登录用户 从登录的时候存的session中获取
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        lqw.orderByAsc(ShoppingCart::getCreateTime);
        //返回集合
        return shoppingCartMapper.selectList(lqw);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @Override
    public int clean() {
        //设置条件 删除当前登录用户的购物车
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        //执行删除
        return shoppingCartMapper.delete(lqw);
    }

    /**
     * 根据用户id查询购物车
     *
     * @param currentId
     * @return
     */
    @Override
    public List<ShoppingCart> selectByUserId(Long currentId) {
        //查询当前购物车数据
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        //查询
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectList(qw);
        return shoppingCartList;
    }

    /**
     * 从购物车移除
     *
     * @param shoppingCart
     * @return
     */
    @Override
    public int subside(ShoppingCart shoppingCart) {
        //构建查询条件
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        lqw.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        //根据提供的id查询数据库
        ShoppingCart cart= shoppingCartMapper.selectOne(lqw);
        //减少数量
        Integer number = cart.getNumber();
        //判断
        if (number > 1) {
            //设置值
            cart.setNumber(number - 1);
            //修改数据库
            return shoppingCartMapper.updateById(cart);
        } else {
            //删除这条记录
            return shoppingCartMapper.delete(lqw);
        }
    }
}
